package com.gxa.medical.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxa.medical.common.Result;
import com.gxa.medical.entity.dto.SupplierQueryDto;
import com.gxa.medical.entity.pojo.Supplier;
import com.gxa.medical.service.SupplierService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//@Controller//IOC注解
//@ResponseBody//返回json数据
@RestController
@RequestMapping("/supplier")
@Api(tags = "供应商相关接口")
public class SupplierController {

    @Autowired
    private SupplierService supplierService;

    @ApiOperation("查询供应商列表，总计记录数")
    @PostMapping("/list")
    public Result list(@RequestBody SupplierQueryDto supplierQueryDto){

        //1.构建查询条件
        QueryWrapper<Supplier> queryWrapper=new QueryWrapper<>();

        if(supplierQueryDto.getSupplierName()!=null && !supplierQueryDto.getSupplierName().equals("")){
            queryWrapper.like("supplier_name",supplierQueryDto.getSupplierName());
        }

        if(supplierQueryDto.getPhone()!=null && !supplierQueryDto.getPhone().equals("")){
            queryWrapper.eq("phone",supplierQueryDto.getPhone());
        }

        //2.构建分页条件
        Page<Supplier> page=new Page<>(supplierQueryDto.getPage(),supplierQueryDto.getLimit());

        //3.查询页面数据
        Page<Supplier> supplierPage = supplierService.page(page, queryWrapper);

        return Result.buildSuccess(supplierPage.getRecords(),supplierPage.getTotal());
    }
    @ApiOperation("根据id查询供应商")
    @GetMapping("/find")
    public Result find(Long id){//springmvc会自动进行参数绑定
        //调用service进行业务处理
        Supplier  supplier =  supplierService.getById(id);
        //返回结果给前端
        return Result.buildSuccess(supplier);
    }

    @ApiOperation("增加供应商")
    @PostMapping("/add")
    public Result add(@RequestBody Supplier supplier){//@RequestBody 前端给后端发送json数据
        System.out.println("从前端接收的信息："+supplier);

        supplierService.save(supplier);
        return Result.buildSuccess();
    }

    @ApiOperation("更新供应商")
    @PostMapping("/update")
    public Result update(@RequestBody Supplier supplier){//@RequestBody 前端给后端发送json数据
        supplierService.updateById(supplier);
        return Result.buildSuccess();
    }

    @ApiOperation("删除供应商")
    @GetMapping("/delete")
    public Result delete(Long id){//springmvc会自动进行参数绑定
        //调用service进行业务处理
        supplierService.removeById(id);
        //返回结果给前端
        return Result.buildSuccess();
    }
}
